﻿using System;
using System.Collections.Generic;
using FoodRaidersWebAPI.domainnew.Article;
using FoodRaidersWebAPI.domainnew.Favorite;
using FoodRaidersWebAPI.domainnew.other;
using FoodRaidersWebAPI.Models;
using FoodRaidersWebAPI.Service.First;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Storage;

namespace FoodRaidersWebAPI.Controllers
{
    [ApiController]
    [Route("[controller]/api")]
    public class UserController : Controller
    {
        private readonly FoodContext context;
        private readonly UserService userService;
        private readonly FoodArticleService foodArticleService;
        private readonly MerchantArticleService merchantArticleService;
        private readonly RaiderArticleService raiderArticleService;

        public UserController(FoodContext dbContext)
        {
            this.context = dbContext;
            userService = new UserService(context);
            foodArticleService = new FoodArticleService(context);
            merchantArticleService = new MerchantArticleService(context);
            raiderArticleService = new RaiderArticleService(context);
        }
        //api 1
        [HttpGet("addFavorite/userid={userid}&faid={faid}")]
        public void addFavoriteFoodArticle(int userid, int faid)
        {
            IDbContextTransaction tran = null;
            try
            {
                //开启事务
                tran = context.Database.BeginTransaction();
                userService.addFavoriteFood(userid, faid);
                foodArticleService.addFavorite(faid);
                //事务提交
                tran.Commit();
            }
            catch (Exception e)
            {
                //事务回滚
                if (tran != null) tran.Rollback();
                throw e;
            }
            finally 
            {
                tran.Dispose();
            }
        }
        //api 2
        [HttpGet("addFavorite/userid={userid}&maid={maid}")]
        public void addFavoriteMerchantArticle(int userid, int maid)
        {
            IDbContextTransaction tran = null;
            try
            {
                //开启事务
                tran = context.Database.BeginTransaction();
                userService.addFavoriteMerchant(userid,maid);
                merchantArticleService.addFavorite(maid);
                //事务提交
                tran.Commit();
            }
            catch (Exception e)
            {
                //事务回滚
                if (tran != null) tran.Rollback();
                throw e;
            }
            finally
            {
                tran.Dispose();
            }
        }
        //api 3
        [HttpGet("addFavorite/userid={userid}&raid={raid}")]
        public void addFavoriteRaiderArticle(int userid, int raid)
        {
            IDbContextTransaction tran = null;
            try
            {
                //开启事务
                tran = context.Database.BeginTransaction();
                userService.addFavoriteRaider(userid,raid);
                raiderArticleService.addFavorite(raid);
                //事务提交
                tran.Commit();
            }
            catch (Exception e)
            {
                //事务回滚
                if (tran != null) tran.Rollback();
                throw e;
            }
            finally
            {
                tran.Dispose();
            }
        }
        //api 4
        [HttpGet("subFavorite/userid={userid}&faid={faid}")]
        public void subFavoriteFoodArticle(int userid, int faid)
        {
            IDbContextTransaction tran = null;
            try
            {
                //开启事务
                tran = context.Database.BeginTransaction();
                userService.subFavoriteFood(userid, faid);
                foodArticleService.subFavorite(faid);
                //事务提交
                tran.Commit();
            }
            catch (Exception e)
            {
                //事务回滚
                if (tran != null) tran.Rollback();
                throw e;
            }
            finally 
            {
                tran.Dispose();
            }
        }
        //api 5
        [HttpGet("subFavorite/userid={userid}&maid={maid}")]
        public void subFavoriteMerchantArticle(int userid, int maid)
        {
            IDbContextTransaction tran = null;
            try
            {
                //开启事务
                tran = context.Database.BeginTransaction();
                userService.subFavoriteMerchant(userid,maid);
                merchantArticleService.subFavorite(maid);
                //事务提交
                tran.Commit();
            }
            catch (Exception e)
            {
                //事务回滚
                if (tran != null) tran.Rollback();
                throw e;
            }
            finally
            {
                tran.Dispose();
            }
        }
        //api 6
        [HttpGet("subFavorite/userid={userid}&raid={raid}")]
        public void subFavoriteRaiderArticle(int userid, int raid)
        {
            IDbContextTransaction tran = null;
            try
            {
                //开启事务
                tran = context.Database.BeginTransaction();
                userService.subFavoriteRaider(userid,raid);
                raiderArticleService.subFavorite(raid);
                //事务提交
                tran.Commit();
            }
            catch (Exception e)
            {
                //事务回滚
                if (tran != null) tran.Rollback();
                throw e;
            }
            finally
            {
                tran.Dispose();
            }
        }
        //api 7
        [HttpGet("find/id={id}")]
        public ActionResult<User> find(int id)
        {
            return userService.findByID(id);
        }
        //api 8
        [HttpGet("FoodArticles/id={id}")]
        public ActionResult<IEnumerable<FoodArticle>> FoodArticles(int id)
        {
            return userService.FoodArticles(id);
        }
        //api 9
        [HttpGet("MerchantArticles/id={id}")]
        public ActionResult<IEnumerable<MerchantArticle>> MerchantArticles(int id)
        {
            return userService.MerchantArticles(id);
        }
        //api 10
        [HttpGet("RaiderArticles/id={id}")]
        public ActionResult<IEnumerable<RaiderArticle>> RaiderArticles(int id)
        {
            return userService.RaiderArticles(id);
        }
        //api 11
        [HttpGet("FavoriteFoodArticles/id={id}")]
        public ActionResult<IEnumerable<FavoriteFoodArticle>> FavoriteFoodArticles(int id)
        {
            return userService.FavoriteFoodArticles(id);
        }
        //api 12
        [HttpGet("FavoriteMerchantArticles/id={id}")]
        public ActionResult<IEnumerable<FavoriteMerchantArticle>> FavoriteMerchantArticles(int id)
        {
            return userService.FavoriteMerchantArticles(id);
        }
        //api 13
        [HttpGet("FavoriteRaiderArticles/id={id}")]
        public ActionResult<IEnumerable<FavoriteRaiderArticle>> FavoriteRaiderArticles(int id)
        {
            return userService.FavoriteRaiderArticles(id);
        }
        
        
        
        
        
        //P_O_S_T
        //api 1
        [HttpPost("addUser")]
        public void addUser(User user)
        {
            if(user==null) return;
            IDbContextTransaction tran = null;
            try
            {
                //开启事务
                tran = context.Database.BeginTransaction();
                userService.addUser(user);
                //事务提交
                tran.Commit();
            }
            catch (Exception e)
            {
                //事务回滚
                if (tran != null) tran.Rollback();
                throw e;
            }
            finally
            {
                tran.Dispose();
            }
        }
    }
}